package com.next.aappublicapp.util;

import java.util.Arrays;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.preference.RingtonePreference;

import com.google.gson.Gson;
import com.next.aap.client.AllItem;
import com.next.aap.client.CandidateSelectionProcess;
import com.next.aap.client.NewsItem;
import com.next.aap.client.VideoItem;

public class PreferencesUtil {

	private final String CACHE_ALL_ITEMS = "AllItemsSharedPreferenceCache";
	private final String CACHE_NEWS = "newsCache";
	private final String CACHE_VIDEOS = "newsVideos";

	private final String CACHE_CSP = "AllCSP";
	private final String CACHE_CSP_ENG = "AllCSPEnglish";
	private final String CACHE_CSP_HIN = "AllCSPHindi";

	private static PreferencesUtil instance = new PreferencesUtil();

	public static PreferencesUtil getInstance() {
		return instance;
	}

	public void saveAllItemCache(final Context context, AllItem allItem) {
		// Make sure the app is registered with GCM and with the server
		try {
			SharedPreferences prefs = context.getSharedPreferences(
					CACHE_ALL_ITEMS, Context.MODE_PRIVATE);
			SharedPreferences.Editor editor = prefs.edit();
			Gson gson = new Gson();
			String newsTring = gson.toJson(allItem.getNewsItems());
			editor.putString(CACHE_NEWS, newsTring);
			String videoString = gson.toJson(allItem.getVideoItems());
			editor.putString(CACHE_VIDEOS, videoString);
			editor.commit();
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	public void saveCandidateSelectionProcessEnglishCache(
			final Context context,
			CandidateSelectionProcess candidateSelectionProcess) {
		// Make sure the app is registered with GCM and with the server
		try {
			SharedPreferences prefs = context.getSharedPreferences(CACHE_CSP,
					Context.MODE_PRIVATE);
			SharedPreferences.Editor editor = prefs.edit();
			Gson gson = new Gson();
			String newsTring = gson.toJson(candidateSelectionProcess);
			editor.putString(CACHE_CSP_ENG, newsTring);
			editor.commit();
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	public void saveCandidateSelectionProcessHindiCache(final Context context,
			CandidateSelectionProcess candidateSelectionProcess) {
		// Make sure the app is registered with GCM and with the server
		try {
			SharedPreferences prefs = context.getSharedPreferences(CACHE_CSP,
					Context.MODE_PRIVATE);
			SharedPreferences.Editor editor = prefs.edit();
			Gson gson = new Gson();
			String newsTring = gson.toJson(candidateSelectionProcess);
			editor.putString(CACHE_CSP_HIN, newsTring);
			editor.commit();
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	public CandidateSelectionProcess getCandidateSelectionProcessCache(final Context context, boolean isEnglish) {
		if(isEnglish){
			return getObjectFromCachedJsoString(context, CACHE_CSP, CACHE_CSP_ENG, CandidateSelectionProcess.class);	
		}else{
			return getObjectFromCachedJsoString(context, CACHE_CSP, CACHE_CSP_HIN, CandidateSelectionProcess.class);
		}
		
	}
	public CandidateSelectionProcess getCandidateSelectionProcessEnglishCache(
			final Context context) {
		return getObjectFromCachedJsoString(context, CACHE_CSP, CACHE_CSP_ENG, CandidateSelectionProcess.class);
	}

	public CandidateSelectionProcess getCandidateSelectionProcessHindiCache(
			final Context context) {
		// Make sure the app is registered with GCM and with the server
		return getObjectFromCachedJsoString(context, CACHE_CSP, CACHE_CSP_HIN, CandidateSelectionProcess.class);
	}

	private <T> T getObjectFromCachedJsoString(Context context,String preferenceName,String preferenceKey,Class<T> clasType) {
		T returnObject = null;
		try {
			SharedPreferences prefs = context.getSharedPreferences(preferenceName,Context.MODE_PRIVATE);
			String csp = prefs.getString(preferenceKey, null);
			if (csp != null) {
				returnObject = new Gson().fromJson(csp,clasType);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return returnObject;
	}

	public AllItem getAllItemCache(final Context context) {
		// Make sure the app is registered with GCM and with the server
		AllItem allItem = new AllItem();
		try {
			SharedPreferences prefs = context.getSharedPreferences(
					CACHE_ALL_ITEMS, Context.MODE_PRIVATE);
			if (prefs == null) {
				return null;
			}
			String newsTring = prefs.getString(CACHE_NEWS, "[]");
			String videoString = prefs.getString(CACHE_VIDEOS, "[]");
			Gson gson = new Gson();

			NewsItem[] newsItems = gson.fromJson(newsTring, NewsItem[].class);
			allItem.setNewsItems(Arrays.asList(newsItems));

			VideoItem[] videoItems = gson.fromJson(videoString,
					VideoItem[].class);
			allItem.setVideoItems(Arrays.asList(videoItems));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return allItem;
	}
	
	public boolean getBooleanSetting(final Context context,String key,boolean defaultValue) {
			SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
			if (prefs == null) {
				return defaultValue;
			}
			return prefs.getBoolean(key, defaultValue);
	}
	
	public String getStringSetting(final Context context,String key,String defaultValue) {
			SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
			if (prefs == null) {
				return defaultValue;
			}
			return prefs.getString(key, defaultValue);
	}

}
